import os
import re
import sys
import requests
from bs4 import BeautifulSoup


class Novel(object):
    def __init__(self, url):
        self.url = url
        req = requests.get(self.url)
        res = req.text
        self.bf = BeautifulSoup(res, "lxml")
        self.download_all = 0

    def get_name(self):
        texts = self.bf.find_all("div", id="info")
        texts = re.findall("<h1>(.*?)</h1>", str(texts))
        Article_name = str(texts)[2:-2]

        return Article_name

    def get_dir(self):
        texts = self.bf.find_all("div", id="list")
        texts = str(re.findall("<dt>(.*?)</dt>", str(texts)))
        texts = re.sub("\'", "", texts)
        Chapter_title = list(texts[1:-1].split(", "))[1:]
        Chapter_title[0] = "《诡秘之主》 第一部 小丑"

        remove_title_length = len(self.get_name()) + 3
        for i in range(len(Chapter_title)):
            Chapter_title[i] = Chapter_title[i][remove_title_length:]

        return Chapter_title

    def get_chapter(self):
        texts = self.bf.find_all("div", id="list")
        texts2 = re.sub("<div(.*?)>|<a(.*?)>|<dt>(.*?)</dt>|<dl(.*?)/center>|<dd>|</dd>|</dl>|</div>", "", str(texts))
        texts2 = re.sub("</a>", ",", texts2)
        Chapter_name = list(texts2[1:-2].split(","))

        texts3 = re.findall("href=\"(.*?)\"", str(texts))
        texts3 = re.sub("\'", "", str(texts3))
        Chapter_link = list(texts3[1:-1].split(", "))[12:]

        all_list = []
        for i in range(len(Chapter_name)):
            temp_list = [Chapter_name[i], Chapter_link[i]]
            all_list.append(temp_list)

        Chapter_list = []
        temp = []

        for j in all_list:
            if j[0][0:3] == "第一章":
                Chapter_list.append(temp)
                temp = []
            temp.append(j)
        Chapter_list.append(temp)

        self.download_all = len(Chapter_link)
        return Chapter_list[1:]

    def get_content(self, link):
        req = requests.get(self.url + link)
        res = req.text
        bf = BeautifulSoup(res, "lxml")
        texts = bf.find_all("div", id="content")
        texts4 = re.sub("<br/>|</br>|<br />|<div(.*?)！|</div>", "", str(texts))
        texts4 = re.sub("\xa0" * 4, "\n", texts4)
        text_content = texts4[1:-1]

        return text_content

    def save_to_file(self, save_path):
        os.mkdir(save_path + self.get_name())
        Chapter_title = self.get_dir()
        Chapter_list = self.get_chapter()
        suffix = str(".txt")

        cnt = 0
        for i in Chapter_title:
            os.makedirs(save_path + self.get_name() + "\\" + i)
            # 进度条
            downloaded = 0
            for j in Chapter_list[cnt]:
                with open(save_path + self.get_name() + "\\" + i + "\\" + j[0] + suffix, "w+", encoding="utf-8") as f:
                    f.write(self.get_content(j[1]))
                    sys.stdout.write("已下载:%.3f%%" % float(downloaded / self.download_all) + '\r')
                    sys.stdout.flush()
                    downloaded += 1
            cnt += 1


if __name__ == "__main__":
    bg = Novel("https://www.xbiquge.so/book/36327/")

    path = input("请输入保存路径：")
    bg.save_to_file(path)
